package com.itheima.web.service.impl.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.pms.CreateWithChildrenDto;
import com.itheima.dto.pms.ProductCreateAttributeIdDto;
import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.pms.PmsProductCategoryService;
import com.itheima.service.pms.PmsProductService;
import com.itheima.vo.pms.Product;
import com.itheima.vo.pms.ProductCategoryParam;
import com.itheima.web.mapper.pms.PmsProductCategoryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, ProductCategoryParam> implements PmsProductCategoryService {
    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    private PmsProductService pmsProductService;

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    /**
     * 添加商品分类
     * @param productCreateAttributeIdDto
     * @return
     */
    @Override
    @Transactional
    public void addProductCreate(ProductCreateAttributeIdDto productCreateAttributeIdDto) {
        ProductCategoryParam productCategoryParam = new ProductCategoryParam();
        BeanUtils.copyProperties(productCreateAttributeIdDto , productCategoryParam);
        this.save(productCategoryParam);
        LambdaQueryWrapper<ProductCategoryParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategoryParam::getName , productCategoryParam.getName());
        Long categoryId = this.getOne(queryWrapper).getId();
        ArrayList<Long> productAttributeIdList = productCreateAttributeIdDto.getProductAttributeIdList();
        if (productAttributeIdList != null) {
            for (Long attributeId : productAttributeIdList) {
                pmsProductCategoryMapper.addProductCategoryAttibute(categoryId , attributeId);
            }
        }
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @Override
    public ResultData deleteCategory(Long id) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getProductCategoryId , id);
        List<Product> list = pmsProductService.list(queryWrapper);
        if (list.size() != 0) {
            return ResultData.error("该分类下有商品，不能删除");
        }else {
            this.removeById(id);
            pmsProductCategoryMapper.removeProductCategoryAttibute(id);
            return ResultData.ok("操作成功" , "");
        }
    }

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    @Override
    public List<CreateWithChildrenDto> listWithChildren() {
        LambdaQueryWrapper<ProductCategoryParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategoryParam::getParentId , 0)
                .eq(ProductCategoryParam::getLevel , 0);
        List<ProductCategoryParam> categoryParamList = this.list(queryWrapper);
        List<CreateWithChildrenDto> createWithChildrenDtoList = categoryParamList.stream().map(item -> {
            CreateWithChildrenDto createWithChildrenDto = new CreateWithChildrenDto();
            BeanUtils.copyProperties(item , createWithChildrenDto);
            LambdaQueryWrapper<ProductCategoryParam> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ProductCategoryParam::getParentId , item.getId())
                    .eq(ProductCategoryParam::getLevel , 1);
            createWithChildrenDto.setChildren(pmsProductCategoryService.list(queryWrapper1));
            return createWithChildrenDto;
        }).collect(Collectors.toList());
        return createWithChildrenDtoList;
    }

    /**
     * 分页查询商品分类
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultData listCategory(Long parentId, Long pageNum, Long pageSize) {
        //创建分页构造器对象
        Page<ProductCategoryParam> pageInfo = new Page<>(pageNum , pageSize);
        LambdaQueryWrapper<ProductCategoryParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategoryParam::getParentId , parentId);
        this.page(pageInfo , queryWrapper);
        PageResult pageResult1 = PageResult.init(pageInfo.getTotal(), pageSize, pageNum, pageInfo.getRecords());
        return ResultData.ok("操作成功" , pageResult1);
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     * @return
     */
    @Override
    public void updateNavStatus(List<Long> ids, Integer navStatus) {
        LambdaQueryWrapper<ProductCategoryParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductCategoryParam::getId , ids);
        List<ProductCategoryParam> list = pmsProductCategoryService.list(queryWrapper);
        list = list.stream().map(item -> {
            item.setNavStatus(navStatus);
            return item;
        }).collect(Collectors.toList());
        pmsProductCategoryService.updateBatchById(list);
    }

    /**
     * 修改显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @Override
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        LambdaQueryWrapper<ProductCategoryParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductCategoryParam::getId , ids);
        List<ProductCategoryParam> list = pmsProductCategoryService.list(queryWrapper);
        list = list.stream().map(item -> {
            item.setShowStatus(showStatus);
            return item;
        }).collect(Collectors.toList());
        pmsProductCategoryService.updateBatchById(list);
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @Override
    public ProductCategoryParam getCategory(Long id) {
        return pmsProductCategoryService.getById(id);
    }

    /**
     * 修改商品分类
     * @param id
     * @param productCategoryParam
     * @return
     */
    @Override
    public void updateCategory(Long id, ProductCategoryParam productCategoryParam) {
        productCategoryParam.setId(id);
        pmsProductCategoryService.updateById(productCategoryParam);
    }


}
